package com.distributedDataBase.controller;

import com.distributedDataBase.pojo.Cart;
import com.distributedDataBase.pojo.Product;
import com.distributedDataBase.pojo.Result;
import com.distributedDataBase.pojo.User;
import com.distributedDataBase.service.CartService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping
    public Result listAll(HttpServletRequest request){
        // 这个user是拦截器解析token后加在request作用域上的
        User user = (User) request.getAttribute("user");

        if (user == null) {
            return new Result("505", "sorry，还没登录");
        }

        //根据用户的id查询这个用户的所有的购物车的信息
        List<Cart> cartList = cartService.listByUserId(user.getId());

        return new Result("200","查询所有购物车", cartList);
    }

    @DeleteMapping
    public Result delete(Integer id, HttpServletRequest request){
        User user = (User) request.getAttribute("user");

        int count = cartService.delCart(id,user.getId());
        if(count==0)
            return new Result("500","删除失败!");
        return new Result("200","删除成功!");
    }

    @PutMapping("/{id}/{total}")
    public Result update(@PathVariable Integer id, @PathVariable Integer total, HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        int rows = cartService.altTotal(id, user.getId(),total);
        if (rows != 0) {
            return new Result("200", "total修改成功", rows);
        }
        return new Result("500", "total修改失败", rows);
    }

    @PostMapping
    public Result add(Product product, HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        Cart cart = new Cart();
        cart.setProductId(product.getId());
        cart.setName(product.getName());
        cart.setPrice(product.getPrice());
        cart.setImgUrl(product.getImgUrl());
        cart.setRemark(product.getRemark());
        cart.setTotal(1);
        cart.setCreateDate(new Date());
        cart.setUserId(user.getId());

        int rows = 0;
        try {
            rows = cartService.addCart(cart);
        } catch (Exception e) {

        }
        if (rows != 0) {
            return new Result("200", "成功添加商品到购物车", rows);
        }
        /*
        如果失败，可能是(商品-用户)是否已经添加过了（我给(product_id,user_id)添加了一个唯一型约束）
        那么看看有没有添加过，已经添加过那么只需商品的数量+1即可
         */
        Cart cart1 = cartService.getByUidAndPid(user.getId(), product.getId());
        int rows1 = 0;
        try {
            rows1 = cartService.altTotal(product.getId(), user.getId(), cart1.getTotal()+1);
        } catch (Exception e) {
        }
        if (rows1 != 0) {
            return new Result("222", "购物车该商品数量加1", rows);
        }
        // 如果+1没成功，那么就添加失败
        return new Result("555", "该商品添加失败", rows);

    }


}
